package com.demo.automic;

import java.math.BigDecimal;
import java.util.ArrayList;

public interface DecimalAccount {
    //获取余额
    BigDecimal getBalance();

    //取款
    void withdraw(BigDecimal amount);

    /*方法内会自动启动1000哥线程，每个线程减十元的操作
     * 如果初始余额为10000，那么正确的结果应该是0
     * */
    static void demo(DecimalAccount account) {
        ArrayList<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            account.withdraw(BigDecimal.TEN);
        }
        threads.forEach(Thread::start);
        threads.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(account.getBalance());
    }
}
